<?php
/**
 * Created by PhpStorm.
 * User: mrlee
 * Date: 2019-02-25
 * Time: 22:20
 */

namespace app\api\controller;


use app\common\controller\Helpinfo;
use app\common\controller\WxCardDate;
use app\common\controller\Wxoption;
use app\common\model\Agent;
use app\common\model\CardGeneral;
use app\common\model\CardMember;
use app\common\model\CardusCodeMaster;
use app\common\model\CardUserGet;
use app\common\model\Merchant;
use app\common\model\WxOpen;
use think\Controller;
use think\Request;
use think\Session;
use Wechat\Wxobjopen;


class WxCardus extends Controller
{



    //会员卡添加
    public function cardus_create($param)
    {
        $Merchant = new Merchant();
        $top_mid  = $Merchant->where(['id'=>$param['merchantid']])->value('top_mid');
        if ($top_mid) {
//        return $param['param_info'];
//        $param = is_array($param)?$param:json_decode($param,true);
            $CardDate = new WxCardDate();
            $datas = $CardDate->carduscreate(json_decode($param['param_info'], true));
//        return $datas;
            $data = json_decode($param['card_info'], true);
            $optionwx = $this->options($param['merchantid'], 'mid');
            $WxObj = new Wxobjopen($optionwx);
            $res = $WxObj->createUserCard($data);
//        return $res;
            if ($res['errcode'] == 0) {
                //END
                $datas['mid'] = !empty($param['merchantid']) ? $param['merchantid'] : 0;
                $datas['top_mid'] = $top_mid;
                $datas['card_id'] = $res['card_id'];
                $datas['cardinfo'] = json_encode($data, JSON_UNESCAPED_UNICODE);
                $CardMember = new CardMember();
                $add = $CardMember->allowField(true)->save($datas);
                $re_param['data']['items'] = $res;
                $re_param['data']['items1'] = $datas;
                if ($add) {
                    $re_param['return_code'] = "SUCCESS";
                    $re_param['return_msg'] = "成功";
                    $re_param['result_code'] = 'SUCCESS';
                    $re_param['result_msg'] = '创建成功';
                } else {
                    $re_param['return_code'] = "SUCCESS";
                    $re_param['return_msg'] = "成功";
                    $re_param['result_code'] = 'FAIL';
                    $re_param['result_msg'] = '保存失败';
                }
            } else {
                $re_param['return_code'] = "SUCCESS";
                $re_param['return_msg'] = "成功";
                $re_param['result_code'] = 'FAIL';
                $re_param['result_msg'] = '创建失败' . $res['errmsg'];
            }
        }else{
            $re_param['return_code'] = "SUCCESS";
            $re_param['return_msg'] = "成功";
            $re_param['result_code'] = 'FAIL';
            $re_param['result_msg'] = '创建失败：未找到商户';
        }
        return $re_param;
    }

    //会员卡修改
    public function cardus_edit($param)
    {

        $CardDate = new WxCardDate();
        $datass = json_decode($param['param_info'],true);
//        return $datass;
        $datas = $CardDate->cardusedit(json_decode($param['param_info'],true));
//        return $datas;
        $data = json_decode($param['card_info'],true);
        $optionwx = $this->options($param['merchantid'],'mid');
        $WxObj = new Wxobjopen($optionwx);
        $res = $WxObj->UpdateUserCard($data);

        if ($res['errcode'] == 0){
            $Merchant = new Merchant();
            //END
            $map['mid'] =  $param['merchantid'];
            $map['top_mid'] =$Merchant->where(['id'=>$param['merchantid']])->value('top_mid');
            $map['card_id'] = $param['card_id'];
            $datas['cardinfo'] = json_encode($data,JSON_UNESCAPED_UNICODE);

            $CardMember = new CardMember();
            $add=$CardMember->allowField(true)->save($datas,$map);
//            return $CardMember->getLastSql();
            $re_param['data']['items'] = $res;
            if ($add){
                $re_param['return_code'] = "SUCCESS";
                $re_param['return_msg'] = "成功";
                $re_param['result_code'] = 'SUCCESS';
                $re_param['result_msg'] = '成功';
            } else {
                $re_param['return_code'] = "SUCCESS";
                $re_param['return_msg'] = "成功";
                $re_param['result_code'] = 'SUCCESS';
                $re_param['result_msg'] = '成功';
            }
        }else{
            $re_param['return_code'] = "SUCCESS";
            $re_param['return_msg'] = "成功";
            $re_param['result_code'] = 'FAIL';
            $re_param['result_msg'] = '修改失败'.$res['errmsg'];
        }
        return $re_param;
    }

    //会员卡详情
    public function cardus_info($param)
    {
        //END
//        $meinfo = $this->getcacheinfo($param['token']);
//        $map['sysid'] = $meinfo['sysid'];
//        $map['id'] = $param['id'];
////        return $map;
//        $CardMember = new CardMember();
//        $info=$CardMember->where($map)->find();
//        if ($info) {
//            $re_param['return_code'] = "SUCCESS";
//            $re_param['return_msg'] = "获取成功";
//            $re_param['data']['items'] = _unsetNull($info);
//        } else {
//            $re_param['return_code'] = "FAIL";
//            $re_param['return_msg'] = "获取失败";
//        }
//        return $re_param;
    }

    //会员卡删除
    public function cardus_del($param)
    {
        $Merchant = new Merchant();
        $machinfo = $Merchant->where(['id'=>$param['merchantid']])->field('id,top_mid')->find();

        $CardMember = new CardMember();
        $map['top_mid'] = $machinfo['top_mid'];
        $map['card_id'] = $param['card_id'];
        $cardinfo = $CardMember->where($map)->field('card_id,id')->find();
        if ($cardinfo) {
            $data['card_id'] = $cardinfo['card_id'];
            $optionwx = $this->options($param['merchantid'],'mid');
            $WxObj = new Wxobjopen($optionwx);
            $res = $WxObj->delUserCard($data);
            $re_param['res']  = $res;
            $res['errcode'] = 0;
            if ($res['errcode'] == 0) {
                $datas['is_del'] = 2;
//                $datas['del_info'] = json_encode($data, JSON_UNESCAPED_UNICODE);
                $info = $CardMember->save($datas, $map);
                if ($info) {
                    $re_param['return_code'] = "SUCCESS";
                    $re_param['return_msg'] = "成功";
                    $re_param['result_code'] = 'SUCCESS';
                    $re_param['result_msg'] = '成功';
                } else {
                    $re_param['return_code'] = "SUCCESS";
                    $re_param['return_msg'] = "成功";
                    $re_param['result_code'] = 'FAIL';
                    $re_param['result_msg'] = '删除失败';
                }
            } else {
                $re_param['return_code'] = "SUCCESS";
                $re_param['return_msg'] = "成功";
                $re_param['result_code'] = 'FAIL';
                $re_param['result_msg'] = '删除失败'. $res['errmsg'];
            }
        }else{
            $re_param['return_code'] = "SUCCESS";
            $re_param['return_msg'] = "成功";
            $re_param['result_code'] = 'FAIL';
            $re_param['result_msg'] = '删除异常';
        }
        return $re_param;
    }

    /*
    * 卡券操作
    *
    */
    //卡券列表
    public function cards_list($param)
    {

    }


    //发布卡券
    public function cards_create($param){

        $CardGeneral = new CardGeneral();
        $CardDate = new WxCardDate();

        $datas = $CardDate->cardscreate(json_decode($param['param_info'],true),$param['card_type'],2);
        $data = json_decode($param['card_info'],true);
        //初始化微信API
        $optionwx = $this->options($param['merchantid'],'mid');
        $WxObj = new Wxobjopen($optionwx);
        $res = $WxObj->createUserCard($data);
//        return $res;
        if ($res['errcode'] == 0) {
            $Merchant = new Merchant();
            $machinfo = $Merchant->where(['id'=>$param['merchantid']])->field('id,top_mid')->find();
//            $datas['create_mid'] = $param['merchantid'];
            $datas['mid'] = $param['merchantid'];
            $datas['top_mid'] = $machinfo['top_mid'];
            $datas['card_id'] = $res['card_id'];
            $datas['is_put'] = 1;
            $datas['put_time'] = time();
            $CardGeneral->allowField(true)->save($datas);
            $re_param['return_code'] = "SUCCESS";
            $re_param['return_msg'] = "成功";
            $re_param['result_code'] = 'SUCCESS';
            $re_param['result_msg'] = '成功';
            $re_param['data']['items'] = $res;
        } else {
            //错误提示翻译
            $Helpinfo = new Helpinfo();
            $errmsg = $Helpinfo->gethelpinfo(0, $res['errcode'], $res['errmsg']);
            //END
            $datas['errmsg'] = $errmsg;

            $re_param['return_code'] = "SUCCESS";
            $re_param['return_msg'] = "成功";
            $re_param['result_code'] = 'FAIL';
            $re_param['result_msg'] = '发布失败'.$errmsg;
        }

        return $re_param;

    }

    //卡券修改
    public function cards_edit($param)
    {
        $CardGeneral = new CardGeneral();
        $CardDate = new WxCardDate();
        $data = $param['card_info'];
//        return $param;
        $datas = $CardDate->cardscreate($param['param_info'],$param['card_type'],2);
//        return $param;
        //初始化微信API
        $optionwx = $this->options($param['merchantid'],'mid');
        $WxObj = new Wxobjopen($optionwx);
        $res = $WxObj->UpdateUserCard($data);
        if ($res['errcode'] == 0) {
            $Merchant = new Merchant();
            $machinfo = $Merchant->where(['id'=>$param['merchantid']])->field('id,top_mid')->find();
//            $datas['create_mid'] = $param['merchantid'];
            $maps['mid'] = $param['merchantid'];
            $maps['top_mid'] = $machinfo['top_mid'];
            $maps['card_id'] = $res['card_id'];
            $datas['is_put'] = 1;
            $datas['put_time'] = time();
            $CardGeneral->allowField(true)->save($datas,$maps);
            $re_param['return_code'] = "SUCCESS";
            $re_param['return_msg'] = "成功";
            $re_param['result_code'] = 'SUCCESS';
            $re_param['result_msg'] = '成功';
            $re_param['data']['items'] = $res;
        } else {
            //错误提示翻译
            $Helpinfo = new Helpinfo();
            $errmsg = $Helpinfo->gethelpinfo(0, $res['errcode'], $res['errmsg']);
            //END
            $datas['errmsg'] = $errmsg;

            $re_param['return_code'] = "SUCCESS";
            $re_param['return_msg'] = "成功";
            $re_param['result_code'] = 'FAIL';
            $re_param['result_msg'] = '发布失败'.$errmsg;
        }
        return $re_param;
    }

    //卡券详情
    public function cards_info($param)
    {
        //END
        $meinfo = $this->getcacheinfo($param['token']);
        $map['sysid'] = $meinfo['sysid'];
        $map['id'] = $param['id'];
        $CardGeneral = new CardGeneral();
        $info=$CardGeneral->where($map)->find();
        if ($info) {
            $re_param['return_code'] = "SUCCESS";
            $re_param['return_msg'] = "成功";
            $re_param['result_code'] = 'SUCCESS';
            $re_param['result_msg'] = '成功';
            $re_param['data']['items'] = _unsetNull($info);
        } else {
            $re_param['return_code'] = "SUCCESS";
            $re_param['return_msg'] = "成功";
            $re_param['result_code'] = 'FAIL';
            $re_param['result_msg'] = '获取失败';
        }
        return $re_param;
    }

    //卡券详情
    public function cards_del($param)
    {
        $Merchant = new Merchant();
        $machinfo = $Merchant->where(['id'=>$param['merchantid']])->field('id,top_mid')->find();

        $CardGeneral = new CardGeneral();
        $map['top_mid'] = $machinfo['top_mid'];
        if (!empty($param['id'])) {
            $map['id'] = $param['id'];
        }
        if (!empty($param['card_id'])) {
            $map['card_id'] = $param['card_id'];
        }
        $cardinfo = $CardGeneral->where($map)->field('card_id,id')->find();
        if ($cardinfo) {
            $data['card_id'] = $cardinfo['card_id'];
            $optionwx = $this->options($param['merchantid'],'mid');
            $WxObj = new Wxobjopen($optionwx);
            $res = $WxObj->delUserCard($data);
            $re_param['res']  = $res;
            $res['errcode'] = 0;
            if ($res['errcode'] == 0) {
                $datas['is_del'] = 2;
//                $datas['del_info'] = json_encode($data, JSON_UNESCAPED_UNICODE);
                $info = $CardGeneral->where(['id'=>$cardinfo['id']])->update($datas);
                if ($info) {
                    $re_param['return_code'] = "SUCCESS";
                    $re_param['return_msg'] = "成功";
                    $re_param['result_code'] = 'SUCCESS';
                    $re_param['result_msg'] = '成功';
                } else {
                    $re_param['return_code'] = "SUCCESS";
                    $re_param['return_msg'] = "成功";
                    $re_param['result_code'] = 'FAIL';
                    $re_param['result_msg'] = '删除失败';
                }
            } else {
                $re_param['return_code'] = "SUCCESS";
                $re_param['return_msg'] = "成功";
                $re_param['result_code'] = 'FAIL';
                $re_param['result_msg'] = '删除失败'. $res['errmsg'];
            }
        }else{
            $re_param['return_code'] = "SUCCESS";
            $re_param['return_msg'] = "成功";
            $re_param['result_code'] = 'FAIL';
            $re_param['result_msg'] = '删除异常';
        }
        return $re_param;
    }

    //微信API参数  根据card_id获取
    public function options($param,$type){
        switch($type){
            case "card_id":
                $CardMember = new CardMember();
                $Merchant = new Merchant();
                $Agent=new Agent();
                $WxOpen=new WxOpen();
                $map['id'] = $CardMember->where('card_id',$param)->value('mid');
                $merchantinfo=$Merchant->where($map)->field('wx_open_id,agent')->find();
                $mab['id'] = $merchantinfo['wx_open_id'];
                $config =$Agent->where(array('id' => $merchantinfo['agent']))->find();
                $users = $WxOpen->where($mab)->field('authorizer_appid,authorizer_refresh_token')->find();
                break;
            case "mid":
                $WxOpen=new WxOpen();
                $Agent=new Agent();
                $Merchant=new Merchant();
                $merchantinfo=$Merchant->where('id',$param)->field('wx_open_id,agent')->find();
                $mab['id'] =$merchantinfo['wx_open_id'];
                $config =$Agent->where(array('id' => $merchantinfo['agent']))->find();
                $users = $WxOpen->where($mab)->find();
                break;

        }


        $options = array(
            'component_token' => $config['component_token'], // 填写你设定的key
            'component_encodingaeskey' => $config['component_encodingaeskey'], // 填写加密用的EncodingAESKey
            'component_appid' => $config['component_appid'], // 填写高级调用功能的app id
            'component_appsecret' => $config['component_appsecret'], // 填写高级调用功能的密钥
            'open_ticket' => $config['open_ticket'],
            'authorizer_appid' => $users['authorizer_appid'],
            'authorizer_refresh_token' => $users['authorizer_refresh_token'],
        );
        return $options;
    }


    //添加会员卡号
    //card_id   name   phone    sex  code  master_id   is_shared   is_child
    public function rec_user_code($param){
        $CardMember = new CardMember();
        $cardinfo  =$CardMember->where(['card_id'=>$param['card_id']])->find();
        if ($cardinfo) {
            $CardUserGet = new CardUserGet();
            $map['card_id'] = $cardinfo['card_id'];
            $map['code'] = $param['code'];
            $userinfo = $CardUserGet->where($map)->find();
            if (!$userinfo) {
                $CardUserGet = new CardUserGet();
                $data['top_mid'] = $cardinfo['top_mid'];
                $data['mid'] = $cardinfo['mid'];
                $data['card_id'] = $cardinfo['card_id'];
                $data['c_id'] = $cardinfo['id'];
                $data['card_type'] = $cardinfo['card_type'];
//                $data['fire'] = 0;          //判断该张会员卡是否自动激活 如果是则将领取状态改为激活   1/0
                //END
                $data['master_id'] = $param['master_id'];
                $data['name'] = $param['name'];
                $data['phone'] = $param['phone'];
                $data['sex'] = $param['sex'];
                $data['address'] = $param['address'];
                $data['code'] = $param['code'];

                $data['is_child'] = $param['is_child'];
                $data['openid'] = $param['openid'];
                $data['have'] = 1;
                $data['is_sync'] = "N";
                $data['is_child'] = "Y";
                $data['is_shared'] =$param['is_shared'];
                $data['is_shared_bonus'] = !empty($param['is_shared_bonus'])?$param['is_shared_bonus']:$param['is_shared'];
                $data['outerstr'] = !empty($param['outerstr'])?$param['outerstr']:"";
                $data['sourec'] = !empty($param['sourec'])?$param['sourec']:"";
                $data['have'] = !empty($param['have'])?$param['have']:0;
                $data['fire'] = !empty($param['fire'])?$param['fire']:0;
                $data['is_sync'] = !empty($param['is_sync'])?$param['is_sync']:"N";
                $data['fire_time'] = !empty($param['fire_time'])?$param['fire_time']:"";
//                $data['is_shared'] = !empty($param['is_shared'])?$param['is_shared']:"N";
                $data['create_time'] = time();

                $res = $CardUserGet->save($data);
                if ($res) {
                    $re_param['return_code'] = "SUCCESS";
                    $re_param['return_msg'] = "成功";
                    $re_param['result_code'] = 'SUCCESS';
                    $re_param['result_msg'] = '成功';
                } else {
                    $re_param['return_code'] = "SUCCESS";
                    $re_param['return_msg'] = "成功";
                    $re_param['result_code'] = 'FAIL';
                    $re_param['result_msg'] = '失败';
                }
            }else{
                $CardUserGet = new CardUserGet();
                $data['master_id'] = $param['master_id'];
                $data['is_child'] = $param['is_child'];
                $data['openid'] = $param['openid'];
                $data['have'] = 1;
                $data['is_child'] = "Y";
                $data['is_shared'] =$param['is_shared'];
                $data['is_shared_bonus'] = !empty($param['is_shared_bonus'])?$param['is_shared_bonus']:$param['is_shared'];
                $data['outerstr'] = !empty($param['outerstr'])?$param['outerstr']:"";
                $data['sourec'] = !empty($param['sourec'])?$param['sourec']:"";
                $data['have'] = !empty($param['have'])?$param['have']:0;
                $data['fire'] = !empty($param['fire'])?$param['fire']:0;
                $data['is_sync'] = !empty($param['is_sync'])?$param['is_sync']:"N";
                $data['fire_time'] = !empty($param['fire_time'])?$param['fire_time']:"";

                $res = $CardUserGet->where(['id'=>$userinfo['id']])->update($data);
                //end
                $re_param['return_code'] = "SUCCESS";
                $re_param['return_msg'] = '成功';
                $re_param['result_code'] = 'FAIL';
                $re_param['result_msg'] = '更新成功已存在此卡号';
            }
        }else{
            $re_param['return_code'] = "SUCCESS";
            $re_param['return_msg'] = '成功';
            $re_param['result_code'] = 'FAIL';
            $re_param['result_msg'] = '失败,没有查询到此卡';
        }
        return $re_param;
    }

    //添加主卡卡号
    //card_id   code    balance     bonus
    public function rec_master_code($param){
        $Mechant = new Merchant();
        $machinfo  =$Mechant->where(['id'=>$param['merchantid']])->find();
        $CardMember = new CardMember();
        $cardinfo  =$CardMember->where(['top_mid'=>$machinfo['top_mid']])->find();
        if (!$cardinfo) {
            $info['top_mid'] = $machinfo['top_mid'];
            $info['card_id'] = "";
            $info['id'] = "";
            $info['card_type'] = "MEMBER_CARD";
        }else{
            $info['top_mid'] = $cardinfo['top_mid'];
            $info['card_id'] =$cardinfo['card_id'];
            $info['id'] = $cardinfo['id'];
            $info['card_type'] = "MEMBER_CARD";
        }
        //END
        $CardusCodeMaster = new CardusCodeMaster();
//        $map['top_mid'] = $info['top_mid'];
        $map['code'] = $param['code'];
        $masterinfo = $CardusCodeMaster->where($map)->find();
        if (!$masterinfo) {
            $data['top_mid'] = $info['top_mid'];
            $data['c_id'] = $info['id'];
            $data['card_type'] = $info['card_type'];
            $data['card_id'] = $info['card_id'];
            //END
            $data['code'] = $param['code'];
            $data['balance'] = $param['balance'];
            $data['bonus'] = $param['bonus'];
            $data['create_time'] = time();
            $res = $CardusCodeMaster->save($data);
            $rec_id = $CardusCodeMaster->getLastInsID();
            if ($res) {
                $re_param['return_code'] = "SUCCESS";
                $re_param['return_msg'] = "成功";
                $re_param['result_code'] = 'SUCCESS';
                $re_param['result_msg'] = '成功';
                $re_param['info']['rec_id'] = $rec_id;
            } else {
                $re_param['return_code'] = "SUCCESS";
                $re_param['return_msg'] = "成功";
                $re_param['result_code'] = 'FAIL';
                $re_param['result_msg'] = '失败';
            }
        }else{
            $re_param['return_code'] = "SUCCESS";
            $re_param['return_msg'] = '成功';
            $re_param['result_code'] = 'FAIL';
            $re_param['result_msg'] = '失败,已存在此卡号';
        }

        return $re_param;
    }

    //查询子卡号信息
    public function query_code_info($param){
        $Mechant = new Merchant();
        $machinfo  =$Mechant->where(['id'=>$param['merchantid']])->find();
        $CardMember = new CardMember();
        $cardinfo  =$CardMember->where(['top_mid'=>$machinfo['top_mid']])->find();
        //是否有会员卡
        if ($cardinfo) {
            $data['card_id'] = $cardinfo['card_id'];
            $data['c_id'] = $cardinfo['id'];
            $data['card_type'] = "MEMBER_CARD";
        }
        //是否有主卡号
        if ($param['is_master'] =="Y") {
            $CardusCodeMaster = new CardusCodeMaster();
            $masterinfo = $CardusCodeMaster->where(['card_id' => $param['card_id'], 'master_code' => $param['master_code']])->find();
            $data['master_id'] = $masterinfo['id'];
        }
        $CardUserGet = new CardUserGet();
        $data['code'] = $param['code'];
        $data['top_mid'] = $machinfo['top_mid'];
        $res = $CardUserGet->where($data)->find();
        if ($res) {
            $re_param['return_code'] = "SUCCESS";
            $re_param['return_msg'] = "成功";
            $re_param['data']['items'] = $res;
        } else {
            $re_param['return_code'] = "FAIL";
            $re_param['return_msg'] = "失败";
        }

        return $re_param;
    }

    //查询主卡号信息
    public function query_mastercode_info($param){
        $CardMember = new CardMember();
        $cardinfo  =$CardMember->where(['card_id'=>$param['card_id']])->find();
        if ($cardinfo) {
            $CardusCodeMaster = new CardusCodeMaster();
            $data['code'] = $param['code'];
            $data['card_id'] = $param['card_id'];
            $res = $CardusCodeMaster->where($data)->find();
            if ($res) {
                $re_param['return_code'] = "SUCCESS";
                $re_param['return_msg'] = "成功";
                $re_param['data']['items'] = $res;
            } else {
                $re_param['return_code'] = "FAIL";
                $re_param['return_msg'] = "失败";
            }
        }else{
            $re_param['return_code'] = "FAIL";
            $re_param['return_msg'] = "失败,没有查询到此卡";
        }
        return $re_param;
    }

    //开关子卡消费权限
    //code card_id is_shared Y/N
    public function user_code_shared_off($param){
        $CardUserGet = new CardUserGet();
        $map['code'] = $param['code'];
        $map['card_id'] = $param['card_id'];
        $user = $CardUserGet->where($map)->find();
        if ($user){
            $data['is_shared'] = $param['is_shared'];            //余额权限
            $data['is_shared_bonus'] = isset($param['is_shared_bonus'])?$param['is_shared_bonus']:$param['is_shared'];       //积分权限
            $res = $CardUserGet->where($map)->update($data);
//            $res=  1;
            //同步积分余额
            $this->user_code_sync($user['id']);
            if ($res){
                $re_param['return_code'] = "SUCCESS";
                $re_param['return_msg'] = "成功";
                $re_param['result_code'] = 'SUCCESS';
                $re_param['result_msg'] = '成功';
                $re_param['data']['items'] = $res;
            } else {
                $re_param['return_code'] = "SUCCESS";
                $re_param['return_msg'] = "成功";
                $re_param['result_code'] = 'SUCCESS';
                $re_param['result_msg'] = '成功';
            }
        }else{
            $re_param['return_code'] = "SUCCESS";
            $re_param['return_msg'] = "成功";
            $re_param['result_code'] = 'FAIL';
            $re_param['result_msg'] = '失败,没有查询到此卡';
        }
        $re_param['result_msg'] =  $re_param['result_msg'].$res['result_msg'];
        return $re_param;
    }

    //开关子卡可用状态
    //code card_id is_shared Y/N
    public function user_code_off($param){
        $CardUserGet = new CardUserGet();
        $map['code'] = $param['code'];
        $map['card_id'] = $param['card_id'];
        $user = $CardUserGet->where($map)->find();
        if ($user){
            $data['have'] = 0;
            $data['fire'] = 0;
            $data['is_shared'] = 'N';
            $data['is_shared_bonus'] = 'N';
            $data['is_del'] = 2;
            $res = $CardUserGet->where($map)->update($data);
            if ($res){
                $this->user_code_sync($user['id']);
                $re_param['return_code'] = "SUCCESS";
                $re_param['return_msg'] = "成功";
                $re_param['result_code'] = 'SUCCESS';
                $re_param['result_msg'] = '成功';
                $re_param['data']['items'] = $res;
            } else {
                $re_param['return_code'] = "SUCCESS";
                $re_param['return_msg'] = "成功";
                $re_param['result_code'] = 'FAIL';
                $re_param['result_msg'] = '失败';
            }
        }else{
            $re_param['return_code'] = "SUCCESS";
            $re_param['return_msg'] = "成功";
            $re_param['result_code'] = 'FAIL';
            $re_param['result_msg'] = '失败,没有查询到此卡';
        }
        $re_param['result_msg'] =  $re_param['result_msg'].$res['result_msg'];
        return $re_param;
    }

    //同步子卡积分余额
    //card_id code
    public function user_code_sync($uid){
        $CardUserGet = new CardUserGet();
        $CardusCodeMaster = new CardusCodeMaster();
        $map['id'] = $uid;
        $user = $CardUserGet->where($map)->find();
        //是否子母卡
        if ($user['is_child'] == 'Y') {
            //拥有余额或积分权限
            $mab['id'] = $user['master_id'];
            $masterinfo = $CardusCodeMaster->where($mab)->find();
            if ($masterinfo) {
                $data['balance'] = $user['is_shared'] == "Y"?$masterinfo['balance']:0;          //余额权限
                $data['bonus'] = $user['is_shared_bonus'] == "Y"?$masterinfo['bonus']:0;        //积分权限
                $res = $CardUserGet->where($map)->update($data);
                if ($res) {
                    //同步卡面
                    $api_param['card_id'] = $user['card_id'];
                    $api_param['code'] = $user['code'];
                    $api_param['num'] = $data['bonus'];
                    $this->updatebonus($api_param);
                    $re_param['return_code'] = "SUCCESS";
                    $re_param['return_msg'] = "成功";
                    $re_param['result_code'] = 'SUCCESS';
                    $re_param['result_msg'] = '成功';
                } else {
                    $re_param['return_code'] = "SUCCESS";
                    $re_param['return_msg'] = "成功";
                    $re_param['result_code'] = 'FAIL';
                    $re_param['result_msg'] = '失败' . $CardUserGet->getLastSql();
                }
            } else {
                $re_param['return_code'] = "SUCCESS";
                $re_param['return_msg'] = "成功";
                $re_param['result_code'] = 'FAIL';
                $re_param['result_msg'] = '失败,没有查询到此卡';
            }

        } else {
            $re_param['return_code'] = "SUCCESS";
            $re_param['return_msg'] = "成功1";
            $re_param['result_code'] = 'SUCCESS';
            $re_param['result_msg'] = '成功1';
        }
        return $re_param;
    }

    //设置单人单张会员卡 / 优惠券的过期失效
    //code card_id merchantid
    public function user_card_code_del($param){
        $CardUserGet = new CardUserGet();
        $map['code'] = $param['code'];
        $map['card_id'] = $param['card_id'];
        $user = $CardUserGet->where($map)->find();
        if ($user){
            $data['code'] = $param['code'];
            $data['card_id'] = $param['card_id'];
            $data['reason'] = $param['reason'];;
            $optionwx = $this->options($param['merchantid'],'mid');
            $WxObj = new Wxobjopen($optionwx);
            $res = $WxObj->delineUserCard($data);
            if ($res['errcode'] == 0) {
                $datas['have'] = 0;
                $datas['is_del'] = 2;
                $CardUserGet->save($datas,['id'=>$user['id']]);
                //END
                $re_param['return_code'] = "SUCCESS";
                $re_param['return_msg'] = "成功";
                $re_param['result_code'] = 'SUCCESS';
                $re_param['result_msg'] = '成功';
            }else{
                $re_param['return_code'] = "SUCCESS";
                $re_param['return_msg'] = "成功";
                $re_param['result_code'] = 'FAIL';
                $re_param['result_msg'] = '失败';
            }
        }else{
            $re_param['return_code'] = "SUCCESS";
            $re_param['return_msg'] = "成功";
            $re_param['result_code'] = 'FAIL';
            $re_param['result_msg'] = '失败,没有查询到此卡';
        }
        return $re_param;
    }

    //code  card_id num
    public function updatebonus($param)
    {
        //实例化微信
        $WxOption = new Wxoption();
        $options = $WxOption->options($param['card_id'],'card_id');
        $WxObj = new Wxobjopen($options);
//        $nums = $param['num'];
        $data = [
            'code' => $param['code'],
            'card_id' => $param['card_id'],
            'record_bonus' => "",               //说明
            'bonus' => $param['num'],           //积分最终值
//            'add_bonus' => $nums,             //积分变动值  负数为减少
            'notify_optional' => [
                'is_notify_bonus' => false,
                'is_notify_balance' => false,
                'is_notify_custom_field1' => false,
            ],
        ];
        $res = $WxObj->updateUserInfo($data);
        return $res;
    }

}